package com.android.camera2.host.app;

import android.Manifest;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
import android.os.HandlerThread;
import android.view.View;
import android.view.WindowManager;

import com.android.camera2.common.settings.CameraPreferenceSetting;
import com.android.camera2.common.utils.CameraParameter;
import com.android.camera2.common.utils.LogUtil;
import com.android.camera2.common.utils.SoundManager;
import com.android.camera2.common.app.IApp;
import com.android.camera2.common.mode.ICameraMode;
import com.android.camera2.common.ui.IAppUi;
import com.android.camera2.R;
import com.android.camera2.host.QuickActivity;
import com.android.camera2.host.mode.PhotoModule;
import com.android.camera2.host.mode.videoModule;
import com.android.camera2.host.ui.CameraAppUi;

import pub.devrel.easypermissions.EasyPermissions;

public class CameraActivity extends QuickActivity implements IApp,View.OnClickListener {

    private static final String TAG = CameraActivity.class.getSimpleName();
    //ui
    private IAppUi mAppUi;
    private boolean mPaused;

    //sound
    SoundManager soundManager;

    Handler mBackgroundHandler;
    Handler mMainHandler;
    HandlerThread mBackgroundThread;

    //camera mode
    private ICameraMode mCurrentMode;

    //camera setting
    private CameraPreferenceSetting preferenceSetting;

    //Camera Permission
    private String[] mPerms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.RECORD_AUDIO};
    private static final int PERMISSION_REQUEST_CODE = 100;

    @Override
    public IAppUi getAppUi() {
        return mAppUi;
    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public Handler getBackgroundHandler() {
        mBackgroundThread = new HandlerThread("Camera Activity back");
        mBackgroundThread.start();
        mBackgroundHandler = new Handler(mBackgroundThread.getLooper());
        return mBackgroundHandler;
    }

    @Override
    public Handler getMainHandler() {
        mMainHandler = new Handler(this.getMainLooper());
        return mMainHandler;
    }

    @Override
    public void releaseBackgroundHandler() {
        if (null != mBackgroundHandler) {
            mBackgroundHandler.removeCallbacksAndMessages(null);
            mBackgroundHandler = null;
        }
        if (null != mBackgroundThread) {
            mBackgroundThread.quitSafely();
        }
    }

    @Override
    public SoundManager getSoundManager() {
        return soundManager;
    }

    @Override
    public CameraAppUi getCameraAppUi() {
        return (CameraAppUi)mAppUi;
    }

    @Override
    protected void onCreateTask() {
        setContentView(R.layout.activity_main);
        if (!EasyPermissions.hasPermissions(this, mPerms)) {
            EasyPermissions.requestPermissions(this, "please check camera permission", PERMISSION_REQUEST_CODE, mPerms);
            return;
        }
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        init();

    }

    private void init() {
        preferenceSetting = new CameraPreferenceSetting();
        mAppUi = new CameraAppUi(this);
        mAppUi.init();
        getCameraAppUi().iv_camera_setting.setOnClickListener(this);
        mAppUi.setOnModuleChangedListener(moduleChagedLisener);
        soundManager = new SoundManager(this);
        mCurrentMode = new PhotoModule(this);
        mCurrentMode.init();
    }

    IAppUi.ModuleChagedLisener moduleChagedLisener = new IAppUi.ModuleChagedLisener() {
        @Override
        public void onModuleChanged(CameraParameter.MODE mode) {
            LogUtil.i(TAG, "current module is " + mode);

            //close module
            closeModule(mCurrentMode);
            //destroy module
            destroyModule(mCurrentMode);
            //switch module
            if (mode == CameraParameter.MODE.PHOTO) {
                mCurrentMode = new PhotoModule(CameraActivity.this);
            } else {
                mCurrentMode = new videoModule(CameraActivity.this);
            }
            //open module
            openModule(mCurrentMode);

        }
    };



    public void closeModule(ICameraMode mode) {
        mode.closeCamera();
        mode.pause();
    }

    public void destroyModule(ICameraMode mode) {
        mode.destroy();
        mAppUi.getPreviewSurface().setVisibility(View.INVISIBLE);
    }

    public void openModule(ICameraMode mode) {
        if (!mPaused) {
            mode.init();
            mAppUi.getPreviewSurface().setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onStartTask() {
        LogUtil.i(TAG, "onStartTask");
    }

    @Override
    protected void onResumeTask() {
        mPaused = false;
        mAppUi.resume();
        mCurrentMode.resume();
    }

    @Override
    protected void onPauseTask() {
        mPaused = true;
        mAppUi.pause();
        mCurrentMode.pause();
    }

    @Override
    protected void onStopTask() {
        LogUtil.i(TAG, "onStopTask");
    }

    @Override
    protected void onDestroyTask() {
        mCurrentMode.destroy();
    }

    @Override
    protected void onReStartTask() {
        LogUtil.i(TAG, "onReStartTask");
        mAppUi.destroy();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_camera_setting:
//                mAppUi.getCoverView().showCover();
                displayCameraSetting();
                LogUtil.i(TAG,"click camera setting ...");
                break;
        }
    }

    //camera preference setting
    public void removeSettingFragment() {
        String tag = CameraPreferenceSetting.class.getSimpleName();
        Fragment settingFragment = getFragmentManager().findFragmentByTag(tag);
        if (settingFragment == null) {
            return;
        }
        if (settingFragment.isAdded()) {
            getFragmentManager().popBackStack(tag, FragmentManager.POP_BACK_STACK_INCLUSIVE);
        }
    }

    private void displayCameraSetting(){
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.replace(R.id.rootView,preferenceSetting,CameraPreferenceSetting.class.getSimpleName());
        transaction.addToBackStack(CameraPreferenceSetting.class.getSimpleName());
        transaction.commit();
    }

}
